﻿#include "changemainpassdialog.h"
#include "ui_changemainpassdialog.h"
#include <QCryptographicHash>
#include <QSettings>
#include <QSqlQuery>
#include <QMessageBox>
#include "dbtools.h"
#include "utils.h"
#include "globdataobj.h"
#include "flatui.h"

changeMainPassDialog::changeMainPassDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::changeMainPassDialog)
{
    ui->setupUi(this);
    this->setWindowTitle("修改主密码");

    initStyle();

    connect(ui->lineEdit_oldPass,SIGNAL(textChanged(QString)),this,SLOT(inputChanged(QString)));
    connect(ui->lineEdit_newPass,SIGNAL(textChanged(QString)),this,SLOT(inputChanged(QString)));
    connect(ui->lineEdit_newPass3,SIGNAL(textChanged(QString)),this,SLOT(inputChanged(QString)));

    connect(ui->pushButton_ok,&QPushButton::clicked,[=](){
        //校验主密码是否正确，
        //判断密码是否输入正确
        QString s = ui->lineEdit_oldPass->text();
        QString ss = QString(QCryptographicHash::hash(s.toUtf8(),QCryptographicHash::Sha256).toHex());
        QSettings sett;
        if(!sett.value("app/mainpass").toString().isEmpty() && sett.value("app/mainpass").toString() == ss){
            //重新加密所有项目
            QString ns = ui->lineEdit_newPass->text();
            QString newPass = QString(QCryptographicHash::hash(ns.toUtf8(),QCryptographicHash::Sha256).toHex());
            resetAllItem(newPass);
        }else{
            QMessageBox::critical(this,"错误","旧密码错误",QMessageBox::Ok,QMessageBox::Ok);
        }

        //重启应用程序
    });
    connect(ui->pushButton_cancel,&QPushButton::clicked,[=](){
        close();
    });
}

changeMainPassDialog::~changeMainPassDialog()
{
    delete ui;
}

void changeMainPassDialog::resetAllItem(QString newPass)
{
    //获取所有分类
    Dbtools *db = new Dbtools;
    QSqlQuery query(db->getDb());
    query.exec("select * from myitems");
    QString s;
    QString rs;
    QString ps;
    QList<QMap<QString,QString>*> list;
    while(query.next()){
        QMap<QString,QString> *map = new QMap<QString,QString>();
        s = query.value("account").toString();
        rs = MyUtils::getDecodStr( s.replace(QRegExp("\\ "),"+"),key);
        ps = MyUtils::getEncodStr(rs,newPass);
        map->insert("id",query.value("id").toString());
        map->insert("account",ps);

        s = query.value("pass").toString();
        rs = MyUtils::getDecodStr( s.replace(QRegExp("\\ "),"+"),key);
        ps = MyUtils::getEncodStr(rs,newPass);
        map->insert("pass",ps);

        list.append(map);

    }
    query.clear();
    //进行更新
    QListIterator<QMap<QString,QString>*> iterList(list);
    while(iterList.hasNext()){
        QMap<QString,QString> *map = iterList.next();
        query.prepare("update myitems set account=:account,pass=:pass,sync_time=sync_time+10,synced='N' where id=:id");
        query.bindValue(":account",map->value("account"));
        query.bindValue(":pass",map->value("pass"));
        query.bindValue(":id",map->value("id"));
        query.exec();
        query.clear();
    }
    QSettings sett;

    sett.setValue("app/mainpass",newPass);
    QMessageBox::StandardButton reply;
    reply = QMessageBox::information(this,tr("提示"),tr("更新主密码后需要重启程序！"),QMessageBox::Yes);
    if(reply == QMessageBox::Yes){
        QApplication *app;
        app->exit(773);
    }
}

void changeMainPassDialog::initStyle()
{
    FlatUI::Instance()->setLineEditQss(ui->lineEdit_oldPass);
    FlatUI::Instance()->setLineEditQss(ui->lineEdit_newPass);
    FlatUI::Instance()->setLineEditQss(ui->lineEdit_newPass3);
    FlatUI::Instance()->setPushButtonQss(ui->pushButton_ok,5, 8, "#1ABC9C", "#E6F8F5", "#2EE1C1", "#FFFFFF", "#16A086", "#A7EEE6");
    FlatUI::Instance()->setPushButtonQss(ui->pushButton_cancel);
}

void changeMainPassDialog::inputChanged(QString text)
{
    ui->pushButton_ok->setEnabled(false);
    if(ui->lineEdit_oldPass->text().isEmpty()){
        return;
    }
    if(ui->lineEdit_newPass->text().isEmpty() || ui->lineEdit_newPass3->text().isEmpty()){
        return;
    }
    if(ui->lineEdit_newPass->text() != ui->lineEdit_newPass3->text()){
        return;
    }
    ui->pushButton_ok->setEnabled(true);
}
